package com.cloud.wms.ui.inside.replenish;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.widget.decoration.LinearVerticalDecoration;
import com.cloud.widget.ScanText;
import com.cloud.widget.utils.DensityUtils;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.ReplenishDownContract;
import com.cloud.wms.ui.inside.entity.ReplenishOrderEntity;
import com.cloud.wms.ui.inside.presenter.ReplenishDownPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-20 15:22
 * @Description: 补货单列表
 */
public class ReplenishOrderActivity extends BaseBarListActivity<ReplenishOrderEntity, ReplenishDownPresenter> implements ReplenishDownContract.View {
    @BindView(R.id.iv_search)
    ImageButton ivSearch;
    @BindView(R.id.scan_text)
    ScanText scanText;
    private String taskCode;

    @Override
    protected String barTitle() {
        return "补货选择";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_replenish_order;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_replenish_order;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        taskCode = getIntent().getStringExtra(IntentKey.INFO_KEY);
        mPresenter.loadReplenishOrder("", page = 0);
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadReplenishOrder("", page = 0);
    }

    @Override
    protected boolean isRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected void loadListData(int page) {
        super.loadListData(page);
        mPresenter.loadReplenishOrder(scanText.getText(), page);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        mPresenter.loadReplenishOrder(result, page = 0);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, ReplenishOrderEntity item) {
        helper.setText(R.id.tv_replenish_code, item.getReplenishCode())
                .setText(R.id.tv_replenish_sku, item.getNeedSKUAmount())
                .setText(R.id.tv_replenish_amount, item.getNeedAmount())
                .setText(R.id.tv_replenish_create, item.getCreateDate());
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<ReplenishOrderEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        mPresenter.receiveTask(mAdapter.getData().get(position).getReplenishID(), taskCode);
        // 调试临时使用
//        TaskEntity taskEntity = new TaskEntity();
//        taskEntity.setBillID(mAdapter.getItem(position).getReplenishID());
//        ReplenishUpCheckActivity.start(this, taskEntity);
    }

    @Override
    protected RecyclerView.ItemDecoration itemDecoration() {
        return new LinearVerticalDecoration(DensityUtils.Dp2Px(this, 10));
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case ReplenishDownPresenter.TASK:
                TaskEntity taskEntity = (TaskEntity) message.obj;
                ReplenishDownBindActivity.start(this, taskEntity);
                break;
        }
    }

    @OnClick({R.id.iv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                String text = scanText.getText();
                mPresenter.loadReplenishOrder(text, page = 0);
                break;
        }
    }

    public static void start(Context context, boolean isTaskFlow) {
        Intent intent = new Intent(context, ReplenishOrderActivity.class);
        intent.putExtra(IntentKey.TYPE_KEY, isTaskFlow);
        context.startActivity(intent);
    }
}
